package com.zeei.app.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.CaseInsensitiveMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zeei.app.dao.PointMonitorDao;
import com.zeei.app.service.PointMonitorService;
import com.zeei.app.vo.PointMonitorVO;

/**
 * @类型名称：PointMonitorServiceImpl
 * @类型描述：排口实时监控Service
 * @功能描述：生命周期---排口实时监控--实现类
 * @创建作者：meng.jy
 * @创建时间: 2018年1月28日
 */
@Service("appPointMonitorServiceImpl")
public class PointMonitorServiceImpl implements PointMonitorService {

	@Autowired
	private PointMonitorDao pointMonitorDao;

	@SuppressWarnings("unchecked")
	@Override
	public JSONObject queryAutoMonitorByHours(PointMonitorVO vo) {

		Assert.notNull(vo, "入参对象不能为空");
		Assert.hasText(vo.getTotalFlags(), "要统计的字段不能为空");
		List<Map<String, Object>> daoList = this.pointMonitorDao.queryAutoMonitorByHours(vo);
		JSONObject jsonO = new JSONObject();
		// echarts---legend
		JSONObject legend = new JSONObject();
		JSONArray legendList = new JSONArray();
		legend.put("data", legendList);
		jsonO.put("legend", legend);
		// echarts---xAxis
		JSONObject xAxis = new JSONObject();
		JSONArray xAxisList = new JSONArray();
		xAxis.put("data", xAxisList);
		xAxis.put("type", "category");
		jsonO.put("xAxis", xAxis);
		// echarts---series
		JSONArray series = new JSONArray();
		jsonO.put("series", series);

		Map<String, Object> indexMap = new HashMap<String, Object>();
		for (int i = 0; daoList != null && i < daoList.size(); i++) {
			int daoListlen = daoList.size();
			Map<String, Object> caseMap = new CaseInsensitiveMap(daoList.get(i));
			Integer hourStr = this.getMapValue(caseMap, "hourStr");
			String dateHour = this.getMapValue(caseMap, "dateHour");
			JSONObject xAxisJson = new JSONObject();
			xAxisJson.put("value", hourStr);
			xAxisJson.put("code", dateHour);
			if (!xAxisList.contains(xAxisJson)) {
				xAxisList.add(xAxisJson);
			}
			String polluteCode = this.getMapValue(caseMap, "polluteCode");
			String polluteName = this.getMapValue(caseMap, "polluteName");
			if (!StringUtils.isBlank(polluteCode)) {
				if (!indexMap.containsKey(polluteCode)) {
					JSONObject legendJson = new JSONObject();
					legendJson.put("name", polluteName);
					legendJson.put("code", polluteCode);
					legendList.add(legendJson);

					JSONObject seriesJson = new JSONObject();
					seriesJson.put("name", polluteName);
					seriesJson.put("type", "line");
					seriesJson.put("smooth", true);
					Double[] seriesData = new Double[daoListlen];
					for (int sk = 0; sk < daoListlen; sk++) {
						seriesData[sk] = 0.0;
					}
					seriesData[i] = this.getMapValue(caseMap, vo.getTotalFlags());
					seriesJson.put("data", seriesData);
					series.add(seriesJson);
					indexMap.put(polluteCode, seriesJson);
				} else {
					JSONObject seriesJson = this.getMapValue(indexMap, polluteCode);
					Double[] seriesData = (Double[]) seriesJson.get("data");
					seriesData[i] = this.getMapValue(caseMap, vo.getTotalFlags());
				}
			}
		}
		return jsonO;
	}

	@SuppressWarnings("unchecked")
	private <T> T getMapValue(Map<String, Object> map, String key) {
		Object ob = map.get(key);
		if (ob != null) {
			return (T) ob;
		}
		return null;
	}
}
